﻿using System;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;

using LTJ.Common;
using LTJ.Config;
using LTJ.Config.Provider;
using LTJ.Entity;

namespace LTJ.FrameWork
{
    /// <summary>
    /// 前台基础页
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 配置信息
        /// </summary>
        protected internal GeneralConfigInfo config;
        /// <summary>
        /// 在线用户信息
        /// </summary>
        protected internal OnlineUserInfo oluserinfo;
        /// <summary>
        /// 当前用户的用户ID
        /// </summary>
        protected internal int userid;
        /// <summary>
        /// 当前用户的在线表ID
        /// </summary>
        protected internal int olid;
        /// <summary>
        /// 当前页面是否被POST请求
        /// </summary>
        protected internal bool ispost;
        /// <summary>
        /// 是否跳转
        /// </summary>
        protected internal bool isrefresh = false;
        /// <summary>
        /// 跳转时间
        /// </summary>
        private int refreshsec = 0;
        /// <summary>
        /// 跳转连接
        /// </summary>
        private string refreshurl = "";
        /// <summary>
        /// 当前页面是否被GET请求
        /// </summary>
        protected internal bool isget;
        /// <summary>
        /// 当前页面标题
        /// </summary>
        protected internal string pagetitle = "页面";
        /// <summary>
        /// 当前日期
        /// </summary>
        protected internal string nowdate;
        /// <summary>
        /// 当前时间
        /// </summary>
        protected internal string nowtime;
        /// <summary>
        /// 当前日期时间
        /// </summary>
        protected internal string nowdatetime;

        protected internal string username = "";
        /// <summary>
        /// 当前用户实体信息
        /// </summary>
        protected UserInfo userinfo = new UserInfo();
        /// <summary>
        /// 当前企业信息
        /// </summary>
        protected CompanyInfo companyinfo = new CompanyInfo();
        /// <summary>
        /// 当前企业管理员权限
        /// </summary>
        protected Authority userauth = new Authority();

        /// <summary>
        /// 控件初始化时计算执行时间
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            m_processtime = (Environment.TickCount - m_starttick) / 1000;
            base.OnInit(e);
        }

        /// <summary>
        /// 得到当前页面的载入时间供模板中调用(单位:毫秒)
        /// </summary>
        /// <returns>当前页面的载入时间</returns>
        public float Processtime
        {
            get { return m_processtime; }
        }

        /// <summary>
        /// 当前页面开始载入时间(毫秒)
        /// </summary>
        public float m_starttick = Environment.TickCount;

        /// <summary>
        /// 当前页面执行时间(毫秒)
        /// </summary>
        public float m_processtime;

        public BasePage()
        {
            config = GeneralConfigs.GetConfig();

            userid = Utils.StrToInt(FrameWork.FrameWorkUtil.GetCookie("userid"), -1);
            username = FrameWorkUtil.GetCookie("username");
            if (userid > 0)
            {
                userinfo = Users.GetUser(userid);
            }

            if (userinfo.status == 1 && FrameWorkUtil.GetCookiePassword(FrameWorkUtil.GetCookie("password"), config.Passwordkey) == userinfo.pass)
            {
                FrameWorkUtil.WriteDYJUserCookies(userinfo);
                companyinfo = Companys.GetCompanyByID(userinfo.cid);
            }

            try
            {
                oluserinfo = OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
            }
            catch
            {
                System.Threading.Thread.Sleep(2000);
                oluserinfo = OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
            }
            if (userid != -1)
            {
                OnlineUsers.UpdateOnlineTime(config.Oltimespan, userid);
            }

            olid = oluserinfo.id;

            ShowPage();
        }

        /// <summary>
        /// 页面处理虚方法
        /// </summary>
        protected virtual void ShowPage()
        {
            return;
        }
    }
}
